package com.kensure.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import co.kensure.frame.ResultInfo;
import co.kensure.frame.ResultRowsInfo;
import co.kensure.http.RequestUtils;
import co.kensure.mem.MapUtils;

import com.alibaba.fastjson.JSONObject;
import com.kensure.mycom.area.model.BaseArea;
import com.kensure.mycom.area.model.BaseCity;
import com.kensure.mycom.area.model.BaseProvince;
import com.kensure.mycom.area.service.BaseAreaService;
import com.kensure.mycom.area.service.BaseCityService;
import com.kensure.mycom.area.service.BaseProvinceService;
import com.kensure.shike.sys.model.SKDict;
import com.kensure.shike.sys.service.SKDictService;

/**
 * 系统级别的数据获取，比如字典，行政区划
 * 
 * @author fankaidi
 *
 */
@Controller
@RequestMapping(value = "sys")
public class SKSysController {

	@Resource
	private BaseProvinceService baseProvinceService;
	@Resource
	private BaseCityService baseCityService;
	@Resource
	private BaseAreaService baseAreaService;

	/**
	 * 字典列表
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "dictlist.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo dictlist(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		int typeid = json.getInteger("typeid");
		List<SKDict> list = SKDictService.getListCache(typeid);
		return new ResultRowsInfo(list);
	}


	/**
	 * 省列表
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "province.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo province(HttpServletRequest req, HttpServletResponse rep) {
		List<BaseProvince> list = baseProvinceService.selectByWhere(null);
		return new ResultRowsInfo(list);
	}

	/**
	 * 市列表
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "city.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo city(HttpServletRequest req, HttpServletResponse rep) {
		String provincecode = req.getParameter("code");
		List<BaseCity> list = baseCityService.selectByWhere(MapUtils.genMap("provincecode", provincecode));
		return new ResultRowsInfo(list);
	}

	/**
	 * 区县列表
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "area.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo area(HttpServletRequest req, HttpServletResponse rep) {
		String citycode = req.getParameter("code");
		String name = req.getParameter("name");
		if(StringUtils.isNotBlank(name)){
			citycode = baseCityService.selectByName(name, "330100").getCode();
		}
		List<BaseArea> list = baseAreaService.selectByWhere(MapUtils.genMap("citycode", citycode));
		return new ResultRowsInfo(list);
	}

}
